<html>

<head>
<title>Mix3D &mdash; Test</title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">

<script type="text/javascript" src="base/mix3d.js"></script>
<script type="text/javascript" src="base/mixCommon.js"></script>
<script type="text/javascript" src="base/mixEvent.js"></script>
<script type="text/javascript" src="base/mixObject.js"></script>
<script type="text/javascript" src="base/mixArchive.js"></script>
<script type="text/javascript" src="math/mixMath.js"></script>
<script type="text/javascript" src="math/mixFloatArray.js"></script>
<script type="text/javascript" src="math/mixVector.js"></script>
<script type="text/javascript" src="math/mixMatrix.js"></script>
<script type="text/javascript" src="math/mixQuat.js"></script>
<script type="text/javascript" src="math/mixPlane.js"></script>
<script type="text/javascript" src="scene/mixBound.js"></script>
<script type="text/javascript" src="scene/mixTransform.js"></script>
<script type="text/javascript" src="scene/mixController.js"></script>
<script type="text/javascript" src="scene/mixSpatial.js"></script>
<script type="text/javascript" src="scene/mixCamera.js"></script>
<script type="text/javascript" src="scene/mixCuller.js"></script>
<script type="text/javascript" src="res/mixColor.js"></script>
<script type="text/javascript" src="res/mixResource.js"></script>
<script type="text/javascript" src="res/mixImage.js"></script>
<script type="text/javascript" src="res/mixTexture.js"></script>
<script type="text/javascript" src="res/mixVertex.js"></script>
<script type="text/javascript" src="res/mixFace.js"></script>
<script type="text/javascript" src="res/mixGeometry.js"></script>
<script type="text/javascript" src="render/mixGLRenderable.js"></script>
<script type="text/javascript" src="render/mixGLRenderDevice.js"></script>
    
<script id="shader-fs" type="x-shader/x-fragment">
    precision mediump float;

    void main(void) {
        gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
    }
</script>

<script id="shader-vs" type="x-shader/x-vertex">
    attribute vec3 aVertexPosition;

    uniform mat4 uMVMatrix;
    uniform mat4 uPMatrix;

    void main(void) {
        gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
    }
</script>


<script type="text/javascript">

    var gl;
    var cam;
    var device;
    function initGL(canvas) {
      
      device = new Mix3D.RenderDeviceGL2(canvas);
      gl = device.gl;
      gl.viewportWidth = canvas.width;
      gl.viewportHeight = canvas.height;
      cam = new Mix3D.PerspCamera(45,canvas.width /canvas.height,0.1, 100.0);
      /*
        try {
            gl = canvas.getContext("experimental-webgl");
            gl.viewportWidth = canvas.width;
            gl.viewportHeight = canvas.height;
            
            cam = new Mix3D.PerspCamera(45,gl.viewportWidth / gl.viewportHeight,0.1, 100.0);
        } catch (e) {
        }
        if (!gl) {
            alert("Could not initialise WebGL, sorry :-(");
        }*/
    }


    function getShader(gl, id) {
        var shaderScript = document.getElementById(id);
        if (!shaderScript) {
            return null;
        }

        var str = "";
        var k = shaderScript.firstChild;
        while (k) {
            if (k.nodeType == 3) {
                str += k.textContent;
            }
            k = k.nextSibling;
        }

        var shader;
        if (shaderScript.type == "x-shader/x-fragment") {
            shader = gl.createShader(gl.FRAGMENT_SHADER);
        } else if (shaderScript.type == "x-shader/x-vertex") {
            shader = gl.createShader(gl.VERTEX_SHADER);
        } else {
            return null;
        }

        gl.shaderSource(shader, str);
        gl.compileShader(shader);

        if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
            alert(gl.getShaderInfoLog(shader));
            return null;
        }

        return shader;
    }


    var shaderProgram;

    function initShaders() {
        var fragmentShader = getShader(gl, "shader-fs");
        var vertexShader = getShader(gl, "shader-vs");

        shaderProgram = gl.createProgram();
        gl.attachShader(shaderProgram, vertexShader);
        gl.attachShader(shaderProgram, fragmentShader);
        gl.linkProgram(shaderProgram);

        if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
            alert("Could not initialise shaders");
        }
        
        if(!shaderProgram.uniformDetails)
        {
          shaderProgram.uniformDetails={};
          var uniforms = gl.getProgramParameter(shaderProgram, gl.ACTIVE_UNIFORMS);
          for (var i=0;i<uniforms;++i) 
          {
            var info=gl.getActiveUniform(shaderProgram, i);
            shaderProgram.uniformDetails[info.name]={loc:gl.getUniformLocation(shaderProgram,info.name),info:info};
          }
        }
        
        if(!shaderProgram.attributeDetials)
        {
          shaderProgram.attributeDetials = {};
          var num = gl.getProgramParameter(shaderProgram, gl.ACTIVE_ATTRIBUTES);
          for (var i=0;i<num; ++i)
          {
            var info = gl.getActiveAttrib(shaderProgram, i);
            shaderProgram.attributeDetials[info.name]={loc:gl.getAttribLocation(shaderProgram,info.name), info:info};
          }
        }
  

        gl.useProgram(shaderProgram);

        shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
        gl.enableVertexAttribArray(shaderProgram.attributeDetials.aVertexPosition.loc);

        //shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
        //shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
    }


    var mvMatrix = new Mix3D.Matrix4();
    var pMatrix = new Mix3D.Matrix4();
    

    function setMatrixUniforms() 
    {
        gl.uniformMatrix4fv(shaderProgram.uniformDetails.uPMatrix.loc, false, cam.projMatrix.FA);
        gl.uniformMatrix4fv(shaderProgram.uniformDetails.uMVMatrix.loc, false, cam.localTransform.getTransformMatrix().FA);
    }



    var triangleVertexPositionBuffer;
    var squareVertexPositionBuffer;

    function initBuffers() {
        triangleVertexPositionBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
        var vertices = [
             0.0,  1.0,  0.0,
            -1.0, -1.0,  0.0,
             1.0, -1.0,  0.0
        ];
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
        triangleVertexPositionBuffer.itemSize = 3;
        triangleVertexPositionBuffer.numItems = 3;

        squareVertexPositionBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
        vertices = [
             1.0,  1.0,  0.0,
            -1.0,  1.0,  0.0,
             1.0, -1.0,  0.0,
            -1.0, -1.0,  0.0
        ];
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
        squareVertexPositionBuffer.itemSize = 3;
        squareVertexPositionBuffer.numItems = 4;
    }
    
    var trans = new Mix3D.Vector3();
    function drawScene() {
        gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
        gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
        
        pMatrix.makePerspective(45, gl.viewportWidth / gl.viewportHeight, 0.1, 100.0);

        mvMatrix.identity();

        mvMatrix.translate(trans.set(-1.5, 0.0, -7.0));
        cam.localTransform.setPosition(0,0,0).translateX(-1.5).translateZ(-7.0);
        gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
        gl.vertexAttribPointer(shaderProgram.attributeDetials.aVertexPosition.loc, triangleVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
        setMatrixUniforms();
        gl.drawArrays(gl.TRIANGLES, 0, triangleVertexPositionBuffer.numItems);
        
        mvMatrix.translate(trans.set(3.0, 0.0, 0.0));
        cam.localTransform.translateX(3.0);
        gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
        gl.vertexAttribPointer(shaderProgram.attributeDetials.aVertexPosition.loc, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
        setMatrixUniforms();
        gl.drawArrays(gl.TRIANGLE_STRIP, 0, squareVertexPositionBuffer.numItems);
    }



    function webGLStart() {
        var canvas = document.getElementById("Mix3D");
        initGL(canvas);
        initShaders();
        initBuffers();

        gl.clearColor(0.0, 0.0, 0.0, 1.0);
        gl.enable(gl.DEPTH_TEST);

        drawScene();
    }


</script>


</head>


<body onload="webGLStart();">
    
    <br />

    <canvas id="Mix3D" style="border: none;" width="500" height="500"></canvas>

    <br/>
    
</body>

</html>
